# 疫情地图可视化
import json
from cProfile import label

from pyecharts.charts import Map
from pyecharts.options import *

f = open("疫情.txt", "r", encoding="UTF-8")
data = f.read()
f.close()
data_dict = json.loads(data)
# 字典中取出省份数据
province_data_list = data_dict["areaTree"][0]["children"]
# 组装成每个省份和确诊人数为元组，并将各个省份的数据封装入列表内
data_list = []
for province_data in province_data_list:
    province_data_tuple = (province_data["name"],province_data["total"]["confirm"])
    data_list.append((province_data_tuple))

# 地图设置
# 创建地图对象
map = Map()
# 添加数据
map.add("各省份确诊人数",data_list,"china")
# 设置全局配置
map.set_global_opts(
    title_opts=TitleOpts(title="全国疫情地图",pos_left="center",pos_bottom="1%"),
    visualmap_opts=VisualMapOpts(       # 视觉映射
        is_show=True,   # 是否显示
        is_piecewise=True,  #是否分段
        pieces=[
            {"min":1,"max":9,"label":"1-9人","color":"#CCFFFF"},
            {"min":100,"max":999,"label":"100-909人","color":"#FFFF99"},
            {"min":1000,"max":4999,"label":"1000-4999人","color":"#FF9966"},
            {"min":5000,"max":9999,"label":"5000-9999人","color":"#FF6666"},
            {"min":10000,"max":99999,"label":"10000-99999人","color":"#CC3333"},
            {"min":100000,"label":"100000-人","color":"#990033"}
        ]

    )
)
# 绘图
map.render("全国疫情地图.html")